import {payKeyboardAsync} from "../../../mycomponent/payKeyboard/PayKeyboardAsync.js";
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
var app = getApp();
Page({
  behaviors: [skinBehavior],
  /**
	 * 页面的初始数据
	 */
  data: {
    pageOptions: {},
    checkInfo: {},
    showTRMember: true,
    showBindMember: false,
    bindMemberStatus: false,

    info_form: {
      transfer_id: "",
      transfer_money: ""
    },
    transfer_id: "",
    memberInfo: "",

    //支付键盘
    payKeyboardShow:false,
    //是否需要支付密码
    need_password:false, 
    //是否设置支付密码
    has_password:false,  
    showConfirm: false //确认转账弹窗
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this.pageOptionsLis = {
      'agency-balance': {
        checkNumApi: 'plugin.agency-balance.frontend.operate-balance.info',
        transferApi: 'plugin.agency-balance.frontend.operate-balance.transfer',
        numText: '---',
        transferName: '受让人',
        transferText: '转账',
        submitKV: {
          uid: 'transfer_id',
          num: 'balance'
        }
      },
      'store-balance-transfer': {
        checkNumApi: 'plugin.store-balance.frontend.operate-balance.info',
        transferApi: 'plugin.store-balance.frontend.operate-balance.transfer',
        numText: '--',
        transferName: '受让人',
        transferText: '转让',
        submitKV: {
          uid: 'recipient_id',
          num: 'trade_value'
        }
      },
      "ycc-money":{
        checkNumApi: 'plugin.ycc-money.frontend.move.index',
        transferApi: 'plugin.ycc-money.frontend.move.move',
        numText: '--',
        transferName: '受让人',
        transferText: '转让',
        submitKV: {
          uid: 'move_uid',
          num: 'amount'
        }
      },
      "acc-points":{
        checkNumApi: 'plugin.acc-points.frontend.records.dash',
        transferApi: 'plugin.acc-points.frontend.records.transfer',
        hiddenTRMember: true,//隐藏受让人输入框
        showBindMember: true,
        showBindMemberOption: {
          name: '绑定uid',
          bindApi: 'plugin.acc-points.frontend.records.bind',
          unbindApi: 'plugin.acc-points.frontend.records.unbind'
        },
        numText: (()=>{
          let basic_info = wx.getStorageSync("yz_basic_info");
          let credit1 = basic_info.lang.member_center?.credit1 || '积分'
          return credit1;
        }),
        transferName: '绑定uid',
        transferText: '划转',
        submitKV: {
          num: 'number'
        }
      }
    };
    let plu = this.options.plu;
    let pageOptions = this.pageOptionsLis[plu];
    this.setData({
      pageOptions: pageOptions,
      showTRMember: pageOptions.hiddenTRMember ? false : true,
      showBindMember: pageOptions.showBindMember ? true : false
    });
    this.getData(true);
   },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {},

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {},

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  getData(init) {
    let urlStr = app.getNetAddresss(this.data.pageOptions.checkNumApi);
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            checkInfo: res.data,
            // need_password: res.data.need_password,
            // has_password: res.data.has_password,
            bindMemberStatus: res.data.third_uid ? true : false
          });
          if(init) {
            let numText = res.data.plugin_name || res.data.page_title;
            let pageTitle = this.data.pageOptions.transferText + this.data.pageOptions.numText;
            if(typeof this.data.pageOptions.numText === 'function') {
              pageTitle = numText;
              numText = this.data.pageOptions.numText();
            }
            this.setData({
              ['pageOptions.numText']: numText,
            });
            wx.setNavigationBarTitle({
              title: pageTitle
            })
          }
          
          
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  bindMemberFn() {
    // 绑定会员id
    let urlStr = app.getNetAddresss(this.data.pageOptions.showBindMemberOption.bindApi);
    app._getNetWork({
      url: urlStr,
      data: { third_uid: this.data.checkInfo.third_uid },
      success: (resdata) => {
        let res = resdata.data;
        wx.showToast({
          icon: 'none',
          title: res.msg,
          duration: 1500
        });
        this.getData();
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  unbindMemberFn() {
    // 解绑会员
    let urlStr = app.getNetAddresss(this.data.pageOptions.showBindMemberOption.unbindApi);
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data;
        wx.showToast({
          icon: 'none',
          title: res.msg,
          duration: 1500
        });
        this.getData();
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },

  payKeyboardComplete(evt){
    payKeyboardAsync.complete(evt.detail);
  },
  getPayKeyboardPassword(){
    this.setData({payKeyboardShow:true});
    return new Promise((resove,reject)=>{
      this.lockClick = false;
      payKeyboardAsync.addCompleteFn((pass)=>{
        resove(pass);
      });
    });
    // let pass = await this.getPayKeyboardPassword();
    // console.log(pass);
  },
  payKeyboardClose(){
    this.setData({payKeyboardShow:false});
  },
  async beforeConfirm() {
    //确认转账前处理
    if (parseFloat(this.data.info_form.transfer_money) > parseFloat(this.data.checkInfo.credit)) {
      wx.showToast({
        icon: 'none',
        title: `${this.data.pageOptions.transferText}数额不可大于您的${this.data.pageOptions.numText}`,
        duration: 1500
      });
      return;
    }
    if (this.data.showTRMember && (this.data.transfer_id == undefined || this.data.transfer_id <= 0 || this.data.transfer_id.length == 0)) {
      wx.showToast({
        icon: 'none',
        title: `${this.data.pageOptions.transferName}id不可为空`,
        duration: 1500
      });
      return;
    }
    if (this.data.showBindMember && !this.data.checkInfo.third_uid) {
      wx.showToast({
        icon: 'none',
        title: `${this.data.pageOptions.showBindMemberOption.name}不能为空`,
        duration: 1500
      });
      return;
    }

    if (this.data.info_form.transfer_money == undefined || this.data.info_form.transfer_money <= 0 || this.data.info_form.length == 0) {
      wx.showToast({
        icon: 'none',
        title: `${this.data.pageOptions.transferText}数额不可低于0`,
        duration: 1500
      });
      return;
    }

    if(this.data.need_password==true && this.data.has_password==false){
      let confirmFlag = await app.confirm("请先设置支付密码");
      if(confirmFlag) {
        wx.navigateTo({
          url: '/packageA/member/set_balance_password/set_balance_password'
        });
      }
      return;
    }
    this.setData({
      showConfirm: true
    });
  },
  //确认转账
  async confirm() {
    if(this.lockClick) return;//防止重复点击
    this.lockClick = true;

    let json = {};
    if(this.data.pageOptions.submitKV.uid) json[this.data.pageOptions.submitKV.uid] = this.data.transfer_id;
    if(this.data.pageOptions.submitKV.num) json[this.data.pageOptions.submitKV.num] = this.data.info_form.transfer_money;

    let urlStr = app.getNetAddresss(this.data.pageOptions.transferApi);
    if(this.data.need_password==true){  //开启支付密码验证
      let pass = await this.getPayKeyboardPassword();
      urlStr += '&password=' + pass;
    }

    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500,
            success: ()=> {
              setTimeout(()=> {
                //要延时执行的代码
                wx.navigateBack({
                  delta: 1,
                  fail: ()=>{
                    this.getData();
                  }
                });
              }, 1500); //延迟时间
            },
          });
          this.lockClick = false;
        } else {
          this.lockClick = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        this.lockClick = false;
        console.log(res);
      }
    });
  },
  transferIdinp(e) {
    let val = e.detail;
    this.setData({
      transfer_id: val
    });
    this.getmemberInfo();
  },
  thirdUidinp(e) {
    let val = e.detail;
    this.setData({
      ['checkInfo.third_uid']: val
    });
  },
  moneyinp(e) {
    let val = e.detail.value;
    this.setData({
      'info_form.transfer_money': val
    });
  },
  getmemberInfo() {
    let urlStr = app.getNetAddresss("member.member.memberInfo");
    urlStr += '&uid=' + this.data.transfer_id;
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          let memberlabel = `昵称：${res.data.nickname || '--'} 姓名：${res.data.realname || '--'}`;
          this.setData({
            memberInfo: res.data,
            memberlabel
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 2000
          });
          this.setData({
            memberInfo: {},
            memberlabel: res.msg
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  }
});
